package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.constant.ShopConstants;
import co.yixiang.modules.shop.domain.YxStorePromote;
import co.yixiang.modules.shop.service.YxStorePromoteService;
import co.yixiang.modules.shop.service.dto.YxStorePromoteDto;
import co.yixiang.modules.shop.service.dto.YxStorePromoteQueryCriteria;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @author frq
* @date 2020-08-05
*/
@Api(tags = "营销活动管理")
@RestController
@RequestMapping("/api/yxStorePromote")
public class YxStorePromoteController {
    @Autowired
	private YxStorePromoteService yxStorePromoteService;

	



	@GetMapping
	@Log("查询促销活动商品表")
	@ApiOperation("查询促销活动商品表")
	public ResponseEntity<Object> getYxStorePromoteProducts(YxStorePromoteQueryCriteria criteria, Pageable pageable) {
		return new ResponseEntity<>(yxStorePromoteService.queryAll(criteria, pageable), HttpStatus.OK);
	}

	@PostMapping
	@Log("新增促销活动商品表")
	@ApiOperation("新增促销活动商品表")
	public ResponseEntity<Object> create(@Validated @RequestBody YxStorePromoteDto resources) {
		return new ResponseEntity<>(yxStorePromoteService.create(resources), HttpStatus.CREATED);
	}

	@PutMapping
	@Log("修改促销活动商品表")
	@ApiOperation("修改促销活动商品表")
	public ResponseEntity<Object> update(@Validated @RequestBody YxStorePromote resources) {
		yxStorePromoteService.update(resources);
		return new ResponseEntity<>(HttpStatus.NO_CONTENT);
	}

	@Log("删除促销活动商品表")
	@ApiOperation("删除促销活动商品表")
	@DeleteMapping
	public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
		yxStorePromoteService.deleteAll(ids);
		return new ResponseEntity<>(HttpStatus.OK);
	}
	@ApiOperation(value = "活动开启/关闭")
	@CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
	@PostMapping(value = "/onsale/{id}")
	public ResponseEntity onSale(@PathVariable Integer id, @RequestBody String jsonStr) {
		JSONObject jsonObject = JSON.parseObject(jsonStr);
		int status = Integer.valueOf(jsonObject.get("status").toString());
		yxStorePromoteService.onSale(id, status);
		return new ResponseEntity(HttpStatus.OK);
	}
}